﻿namespace com.stifel.BranchMgmt.WebParts.PromoteInfo
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.UI.WebControls;

    using com.stifel.BranchMgmtDao;
    using com.stifel.Web;

    using Microsoft.SharePoint;

    public partial class PromoteInfoUserControl
    {
        #region Fields

        List<uiBusinessUnit_Data> _uiBusinessUnits_Data_List;

        #endregion Fields

        #region Properties

        List<uiBusinessUnit_Data> uiBusinessUnits_Data_List
        {
            get
            {
                if ( _uiBusinessUnits_Data_List == null )
                    _uiBusinessUnits_Data_List = new List<uiBusinessUnit_Data>( );
                return _uiBusinessUnits_Data_List;
            }
            set
            {
                _uiBusinessUnits_Data_List = value;
            }
        }

        #endregion Properties

        #region Methods

        void loadBasics( Guid ItemGuid )
        {
            StifelBranchInfoDao dao = new StifelBranchInfoDao( SPContext.Current.Web.Url );
            StifelBranchInfo value = dao.Get( ItemGuid );
            if ( value != null )
            {
                uiDivBasics.Visible = true;
                uiDivBasics_NA.Visible = false;
                uiBranchId.Text = value.BranchLocationIdentifier;
                loadBranchAddress( value );
                uiSiteId.Text = string.IsNullOrEmpty( value.SiteId ) ? string.Empty : value.SiteId.Trim( );
                uiSpeedDial.Text = string.IsNullOrEmpty( value.SpeedDial ) ? string.Empty : value.SpeedDial.Trim( );
                uiThompsonIdPrefix.Text = string.IsNullOrEmpty( value.ThompsonIdPrefix ) ? string.Empty : value.ThompsonIdPrefix.Trim( );
                uiTimeZone.Text = value.TimeZoneLookupusr_tz_value;
                uiBroadcastIp.Text = value.BroadCastIPAsHtml;
                uiBranchType.Text = value.BranchTypeTitle;
                uiPriceServer.Text = value.PriceServer;
                loadBusinessUnits( value );
            }
            else
            {
                uiDivBasics.Visible = false;
                uiDivBasics_NA.Visible = true;
                uiLblBasics_NA.Text = "<h4>The specified branch could not be found within this site. Perhaps the branch has already been promoted.</h4>";
            }
        }

        void loadBranchAddress( StifelBranchInfo value )
        {
            HtmlDivisionControl div = new HtmlDivisionControl( );
            StringBuilder sb = new StringBuilder( );
            if ( !string.IsNullOrEmpty( value.BranchStreetAddress ) )
                sb.AppendFormat( "{0}\t", value.BranchStreetAddress );
            if ( !string.IsNullOrEmpty( value.BranchSuiteAddress ) )
                sb.AppendFormat( "{0}\t", value.BranchSuiteAddress );
            if ( !string.IsNullOrEmpty( value.BranchCity ) )
                sb.AppendFormat( "{0},", value.BranchCity );
            if ( !string.IsNullOrEmpty( value.USStatesLookupusr_us_value ) )
                sb.AppendFormat( " {0}", value.USStatesLookupusr_us_value );
            if ( !string.IsNullOrEmpty( value.BranchZipcode ) )
                sb.AppendFormat( " {0}", value.BranchZipcode );
            string s = sb.ToString( ).Trim( ).Replace( "\t", "<br />" );
            sb = null;
            uiLblValAddress.Text = s;
        }

        void loadBusinessUnits( StifelBranchInfo value )
        {
            StifelBranchUnitDao dao = new StifelBranchUnitDao( SPContext.Current.Web.Url );
            List<StifelBranchUnit> sbuList = new List<StifelBranchUnit>( );
            if ( value.Id.HasValue )
                sbuList = dao.GetByBranchLocation( value.Id.Value );
            if ( ( sbuList != null ) && ( sbuList.Count != 0 ) )
            {
                foreach ( StifelBranchUnit sbu in sbuList )
                {
                    uiBusinessUnits_Data_List.Add( new uiBusinessUnit_Data
                    {
                        BusinessUnit = sbu.Title,
                        CheckCode = sbu.CheckCodesAsHtml,
                        DestCode = sbu.DestinationCodesAsHtml,
                        ORG = sbu.ORG,
                        WireCode = sbu.WireCodesAsHtml,
                        value = sbu.UniqueId.ToString( )
                    } );
                }
            }
            if ( ( uiBusinessUnits_Data_List != null ) && ( uiBusinessUnits_Data_List.Count != 0 ) )
            {
                uiBusinessUnit_Repeater.Visible = true;
                uiBusinessUnit_Repeater.DataSource = uiBusinessUnits_Data_List;
                uiBusinessUnit_Repeater.DataBind( );
            }
            else
            {
                uiBusinessUnit_Repeater.Visible = false;
            }
        }

        #endregion Methods

        #region Nested Types

        protected class uiBusinessUnit_Data
        {
            #region Properties

            public string BusinessUnit
            {
                get;
                set;
            }

            public string CheckCode
            {
                get;
                set;
            }

            public string DestCode
            {
                get;
                set;
            }

            public string ORG
            {
                get;
                set;
            }

            public string value
            {
                get;
                set;
            }

            public string WireCode
            {
                get;
                set;
            }

            #endregion Properties
        }

        #endregion Nested Types
    }
}